<template>
  <el-dialog
    title="分配角色"
    :visible="visible"
    width="50%"
    @close="closeRoleDialog"
    @open="openDialog"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer">
      <el-button @click="closeRoleDialog">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </span>
  </el-dialog>

</template>

<script>
import { getRoleListAPI } from '@/api/settinng'
import { assignRolesAPI } from '@/api/employees'
import { getUserInfoAPI } from '@/api/uesr'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [],
      roleList: []
    }
  },
  methods: {
    async openDialog() {
      const result = await getUserInfoAPI(this.id)
      this.checkList = result.data.roleIds
      const res = await getRoleListAPI({ page: 1, pagesize: 100 })
      this.roleList = res.data.rows
    },
    closeRoleDialog() {
      this.$emit('close')
    },
    async confirm() {
      const res = await assignRolesAPI({ roleIds: this.checkList, id: this.id }).catch(err => console.log(err))
      this.$message.success(res.message)
      this.closeRoleDialog()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
